#!/home/python/.virtualenvs/open/bin/python

import sys
sys.path.append('./')
sys.path.insert(0, "../")

import os
if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'equity_mall.settings.prod'

import django
django.setup()

import requests

from merchants.models import *
from orders.models import OrderInfo


def get_merchant_eight_eight_table():
    whitetable = list(MerchantEightEightWhiteTable.objects.filter(activity_id=3).values_list('business_no', flat=True))
    query_set = Merchant.objects.filter(level__district_id=9897).exclude(wyfMerchantID__in=whitetable)
    body = {
        "shopId": "",
        "start": "2023-12-01 00:00:00",
        "end": "2023-12-31 23:59:59",
        "channel": "all",
        "orderStatus": "all",
        "orderType": "all",
        "payType": "all",
        "agentId": "all",
        "branchId": "2",
        "account": "gdyouzheng",
    }
    url = 'https://manage.gdwxyf.com/internal/searchorders.do'
    for query in query_set:
        body['shopId'] = query.wyfMerchantID
        response = requests.post(url=url, data=body)
        result = response.json()
        totalNormal = result['statistics']['totalNormal']
        totalCount = result['statistics']['totalCount']
        if totalCount >= 8 and totalNormal >= 16:
            merchanteighteightwhitetable_data = {}
            merchanteighteightwhitetable_data['business_no'] = query.wyfMerchantID
            merchanteighteightwhitetable_data['activity_id'] = 3
            merchanteighteightwhitetable_data['is_win_type'] = 1
            merchanteighteightwhitetable_data['win_type'] = 11
            merchanteighteightwhitetable, create = MerchantEightEightWhiteTable.objects.get_or_create(business_no=query.wyfMerchantID, activity_id=3, defaults=merchanteighteightwhitetable_data)
            print(query.wyfMerchantID)


from orders.models import OrderInfo, OrderProfitSharing
import datetime
from random import randint
from equity_mall.utils.utils import get_level_list, random_str, ryx_settle_order, ryx_profitsharing_order


def wash_orderinfo():
    order = OrderInfo.objects.get(id=5232107)
    if order.is_profit_sharing == 1:
        out_trade_no = f'{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}{"%06d" % randint(0, 999999)}'
        body = {
            "program_id": "202316837141378884",
            "shop_id": order.merchant.ruiyinxinMerchantID,
            "order_id": out_trade_no,
            "out_trade_no": order.orderNum,
            "is_profit": "0"
        }
        result = ryx_profitsharing_order(body)
        profitorder = OrderProfitSharing.objects.filter(order_id=order.id).first()
        if not profitorder:
            profitorder, create = OrderProfitSharing.objects.get_or_create(defaults={'order_id': order.id, "out_trade_no": out_trade_no}, order_id=order.id)
        if result['result_code'] == 'success':
            order.is_profit_sharing = 2
            order.save()
            profitorder.profit_order_id = result['profit_order_id']
            profitorder.profit_time = datetime.datetime.now()
            profitorder.save()
        else:
            order.is_profit_sharing = 2
            order.save()
            profitorder.error_msg = result['error_msg']
            profitorder.profit_status = 2
            profitorder.save()


if __name__ == '__main__':
    wash_orderinfo()